<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import * as echarts from 'echarts';

// 静态数据
const cardData = ref({
  payOrder: { value: 51, total: 51, title: '支付订单', unit: '年' },
  orderCount: { value: 555, rate: '60%', title: '订单量', unit: '周' },
  sales: { value: 4, total: 3.74, title: '销售额', unit: '年' },
  newUsers: { value: 21, total: 21, title: '新增用户', unit: '年' }
});

const menuItems = [
  { icon: 'user', text: '用户', color: '#1890ff' },
  { icon: 'shopping', text: '商品', color: '#722ed1' },
  { icon: 'file', text: '订单', color: '#eb2f96' },
  { icon: 'comment', text: '评价', color: '#13c2c2' },
  { icon: 'picture', text: '图库', color: '#fa8c16' },
  { icon: 'notification', text: '公告', color: '#52c41a' },
  { icon: 'setting', text: '配置', color: '#2f54eb' },
  { icon: 'gift', text: '优惠券', color: '#fa541c' }
];

const storeData = ref({
  onAudit: 12,
  onSale: 9,
  offShelf: 18,
  lowStock: 0
});

const tradeData = ref({
  waitPay: 171,
  waitDeliver: 16,
  delivered: 1,
  refunding: 17
});

// 时间范围类型
type TimeRange = 'month' | 'week' | 'day';

// 订单图表数据
const chartTimeRange = ref<TimeRange>('month');
const orderChartData: Record<TimeRange, number[]> = {
  month: [50, 1, 1, 1, 1, 1, 1],
  week: [30, 25, 20, 15, 10, 5, 2],
  day: [10, 8, 6, 5, 4, 3, 2]
};

const chartLabels: Record<TimeRange, string[]> = {
  month: ['07-24', '07-23', '07-22', '07-21', '07-20', '07-19', '07-18'],
  week: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  day: ['00:00', '04:00', '08:00', '12:00', '16:00', '20:00', '23:00']
};

// 初始化订单图表
let orderChart: echarts.ECharts | null = null;

const initOrderChart = () => {
  const chartDom = document.getElementById('orderChart');
  if (!chartDom) return;

  orderChart = echarts.init(chartDom);
  updateOrderChart();
};

const updateOrderChart = () => {
  if (!orderChart) return;

  const option = {
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '10%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: chartLabels[chartTimeRange.value],
      axisLine: {
        lineStyle: {
          color: '#000000'
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: false,
        lineStyle: {
          color: '#000000'
        }
      },
      axisTick: {
        show: false
      },
      splitLine: {
        lineStyle: {
          color: '#eee'
        }
      },
    
    },
    series: [
      {
        data: orderChartData[chartTimeRange.value],
        type: 'bar',
        barWidth: '30%',
        itemStyle: {
          color: '#4050B5',
          borderRadius: [20, 20, 0, 0]
        }
      }
    ]
  };

  orderChart.setOption(option);
};

const changeTimeRange = (range: TimeRange) => {
  chartTimeRange.value = range;
  updateOrderChart();
};

onMounted(() => {
  initOrderChart();

  // 窗口大小变化时重绘图表
  window.addEventListener('resize', () => {
    orderChart && orderChart.resize();
  });
});
</script>

<template>
  <div class="dashboard">
    <!-- 顶部数据卡片 -->
    <div class="data-cards">
      <div class="card">
        <div class="card-header">
          <span>支付订单</span>
          <span class="unit">{{ cardData.payOrder.unit }}</span>
        </div>
        <div class="card-number">{{ cardData.payOrder.value }}</div>
        <div class="card-footer">
          <span>总支付订单</span>
          <span>{{ cardData.payOrder.total }}</span>
        </div>
      </div>

      <div class="card">
        <div class="card-header">
          <span>订单量</span>
          <span class="unit">{{ cardData.orderCount.unit }}</span>
        </div>
        <div class="card-number">{{ cardData.orderCount.value }}</div>
        <div class="card-footer">
          <span>转化率</span>
          <span>{{ cardData.orderCount.rate }}</span>
        </div>
      </div>

      <div class="card">
        <div class="card-header">
          <span>销售额</span>
          <span class="unit">{{ cardData.sales.unit }}</span>
        </div>
        <div class="card-number">{{ cardData.sales.value }}</div>
        <div class="card-footer">
          <span>总销售额</span>
          <span>{{ cardData.sales.total }}</span>
        </div>
      </div>

      <div class="card">
        <div class="card-header">
          <span>新增用户</span>
          <span class="unit">{{ cardData.newUsers.unit }}</span>
        </div>
        <div class="card-number">{{ cardData.newUsers.value }}</div>
        <div class="card-footer">
          <span>总用户</span>
          <span>{{ cardData.newUsers.total }}</span>
        </div>
      </div>
    </div>

    <!-- 功能菜单 -->
    <div class="menu-cards">
      <div class="menu-item" v-for="(item, index) in menuItems" :key="index">
        <div class="menu-icon" :style="{ color: item.color }">
          <i :class="'icon-' + item.icon"></i>
        </div>
        <div class="menu-text">{{ item.text }}</div>
      </div>
    </div>

    <!-- 数据统计区域 -->
    <div class="stats-container">
      <!-- 订单统计 -->
      <div class="stats-card order-stats">
        <div class="stats-header">
          <div class="stats-title">订单统计</div>
          <div class="time-range">
            <span :class="{ active: chartTimeRange === 'month' }" @click="changeTimeRange('month')">近1个月</span>
            <span :class="{ active: chartTimeRange === 'week' }" @click="changeTimeRange('week')">近1周</span>
            <span :class="{ active: chartTimeRange === 'day' }" @click="changeTimeRange('day')">近24小时</span>
          </div>
        </div>
        <div id="orderChart" class="chart-container"></div>
      </div>

      <!-- 店铺及商品展示 -->
      <div style="width: 94.5%;display: flex;flex-direction: column;">
        <div class="stats-card store-stats">
          <div class="stats-header">
            <div class="stats-title">店铺及商品展示</div>
            <div class="stats-action">店铺及商品展示</div>
          </div>

          <div class="store-data">
            <div class="store-item">
              <div class="store-value">{{ storeData.onAudit }}</div>
              <div class="store-label">审核中</div>
            </div>
            <div class="store-item">
              <div class="store-value">{{ storeData.onSale }}</div>
              <div class="store-label">销售中</div>
            </div>
            <div class="store-item">
              <div class="store-value">{{ storeData.offShelf }}</div>
              <div class="store-label">已下架</div>
            </div>
            <div class="store-item">
              <div class="store-value">{{ storeData.lowStock }}</div>
              <div class="store-label">库存预警</div>
            </div>
          </div>
        </div>
        <div class="stats-card store-stats" style="margin-top: 10px;">
          <div class="stats-header">
            <div class="stats-title">交易展示</div>
            <div class="stats-action">需要立即处理的交易订单</div>
          </div>
          <div class="trade-data">
            <div class="trade-item">
              <div class="trade-value">{{ tradeData.waitPay }}</div>
              <div class="trade-label">待付款</div>
            </div>
            <div class="trade-item">
              <div class="trade-value">{{ tradeData.waitDeliver }}</div>
              <div class="trade-label">待发货</div>
            </div>
            <div class="trade-item">
              <div class="trade-value">{{ tradeData.delivered }}</div>
              <div class="trade-label">已发货</div>
            </div>
            <div class="trade-item">
              <div class="trade-value">{{ tradeData.refunding }}</div>
              <div class="trade-label">退款中</div>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<style lang="scss" scoped>
.dashboard {
  // padding: 20px;
  background-color: #f0f2f5;
  box-sizing: border-box;
}

// 数据卡片样式
.data-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 20px;

  .card {
    background-color: #fff;
    padding: 20px;
    border-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);

    .card-header {
      display: flex;
      justify-content: space-between;
      color: #666;
      font-size: 14px;

      .unit {
        display: inline-block;
        padding: 2px 8px;
        border: 1px solid #e8e8e8;
        border-radius: 15px;
        font-size: 12px;
      }
    }

    .card-number {
      font-size: 36px;
      font-weight: 500;
      margin: 15px 0;
      color: #333;
    }

    .card-footer {
      display: flex;
      justify-content: space-between;
      color: #666;
      font-size: 14px;
    }
  }
}

// 菜单样式
.menu-cards {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 15px;
  margin-bottom: 20px;

  .menu-item {
    background-color: #fff;
    padding: 20px 10px;
    text-align: center;
    border-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
    }

    .menu-icon {
      font-size: 24px;
      margin-bottom: 8px;

      i {
        font-size: 28px;
      }
    }

    .menu-text {
      font-size: 14px;
      color: #666;
    }
  }
}

// 统计区域样式
.stats-container {
  display: grid;
  grid-template-columns: 65% 35%;
  gap: 20px;
  margin-bottom: 20px;
}

.stats-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 20px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: linear-gradient(90deg, #4050B5, #7986CB);
  }

  &:hover {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    transform: translateY(-2px);
  }

  .stats-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 15px;
    border-bottom: 1px solid #f0f0f0;
    margin-bottom: 15px;
    padding: 0 15px;
    padding-top: 10px;

    .stats-title {
      font-size: 16px;
      font-weight: 600;
      color: #333;
      position: relative;
      padding-left: 12px;

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 4px;
        height: 16px;
        background: linear-gradient(to bottom, #4050B5, #7986CB);
        border-radius: 2px;
      }
    }

    .time-range {
      span {
        display: inline-block;
        padding: 5px 10px;
        margin-left: 5px;
        border-radius: 15px;
        font-size: 13px;
        color: #666;
        background-color: #f5f5f5;
        cursor: pointer;
        transition: all 0.3s ease;

        &:hover {
          background-color: #f0f0f0;
        }

        &.active {
          background-color: #e6f7ff;
          color: #1890ff;
          box-shadow: 0 2px 6px rgba(24, 144, 255, 0.2);
        }
      }
    }

    .stats-action {
      color: #ff4d4f;
      border: 1px solid rgba(255, 77, 79, 0.3);
      padding: 6px 12px;
      border-radius: 16px;
      font-size: 12px;
      font-weight: 500;
      background-color: rgba(255, 77, 79, 0.05);
      transition: all 0.3s;
      cursor: pointer;

      &:hover {
        background-color: rgba(255, 77, 79, 0.1);
        border-color: rgba(255, 77, 79, 0.5);
      }
    }
  }
}

// 图表容器
.chart-container {
  height: 300px;
}

// 店铺数据
.store-data,
.trade-data {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  text-align: center;

  .store-value,
  .trade-value {
    font-size: 32px;
    font-weight: 600;
    margin-bottom: 12px;
    background: linear-gradient(135deg, #1a237e 0%, #4050B5 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    position: relative;

    &::after {
      content: '';
      position: absolute;
      bottom: -8px;
      left: 50%;
      transform: translateX(-50%);
      width: 30px;
      height: 3px;
      background: linear-gradient(90deg, #4050B5, #7986CB);
      border-radius: 3px;
    }
  }

  .store-label,
  .trade-label {
    color: #666;
    font-size: 14px;
    font-weight: 500;
  }
}

.store-item,
.trade-item {
  position: relative;
  transition: all 0.3s ease;
  border-radius: 8px;

  &:hover {
    background-color: rgba(240, 242, 245, 0.5);
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.05);
  }

  &::before {
    content: '';
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    width: 1px;
    height: 60%;
    background: linear-gradient(to bottom, transparent, #e8e8e8, transparent);
  }

  &:last-child::before {
    display: none;
  }
}

// 交易展示
.trade-container {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 20px;
  margin-bottom: 20px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: linear-gradient(90deg, #4050B5, #7986CB);
  }

  &:hover {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    transform: translateY(-2px);
  }
}

// 页脚
.footer {
  text-align: center;
  color: #666;
  font-size: 14px;
  padding: 15px 0;
}

// 简单图标样式
[class^="icon-"] {
  display: inline-block;
  width: 24px;
  height: 24px;
}

.icon-user:before {
  content: "👤";
}

.icon-shopping:before {
  content: "🛒";
}

.icon-file:before {
  content: "📄";
}

.icon-comment:before {
  content: "💬";
}

.icon-picture:before {
  content: "🖼";
}

.icon-notification:before {
  content: "📢";
}

.icon-setting:before {
  content: "⚙";
}

.icon-gift:before {
  content: "🎁";
}

.order-stats{
padding: 0;
}
</style>
